package com.cincc.cinccimdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.model.MessageInfo;
import com.cincc.cinccimdemo.model.MessageInfoUI;
import com.cincc.cinccimdemo.viewholder.MessageContentHolder;
import com.cincc.cinccimdemo.viewholder.RecallHolder;
import com.cincc.cinccimdemo.viewholder.ReceiveAudioHolder;
import com.cincc.cinccimdemo.viewholder.ReceiveCardHolder;
import com.cincc.cinccimdemo.viewholder.ReceiveFileHolder;
import com.cincc.cinccimdemo.viewholder.ReceiveImageViewHolder;
import com.cincc.cinccimdemo.viewholder.ReceiveSequenceViewHolder;
import com.cincc.cinccimdemo.viewholder.ReceiveTextViewHolder;
import com.cincc.cinccimdemo.viewholder.ReceiveVideoHolder;
import com.cincc.cinccimdemo.viewholder.SendAudioHolder;
import com.cincc.cinccimdemo.viewholder.SendCardHolder;
import com.cincc.cinccimdemo.viewholder.SendFileHolder;
import com.cincc.cinccimdemo.viewholder.SendImageViewHolder;
import com.cincc.cinccimdemo.viewholder.SendSequenceViewHolder;
import com.cincc.cinccimdemo.viewholder.SendTextViewHolder;
import com.cincc.cinccimdemo.viewholder.SendVideoHolder;
import com.cincc.cinccimdemo.viewholder.TextViewHolder;
import com.cincc.cinccimdemo.viewmodel.ConversationViewModel;

import java.util.List;

public class ConversationAdapter extends RecyclerView.Adapter<MessageContentHolder> {

    ConversationViewModel conversationViewModel;
    private Context context;
    private final List<MessageInfoUI> list;

    private static final int CONVERSATION_SEND_TEXT = 0;
    private static final int CONVERSATION_RECEIVE_TEXT = 1;
    private static final int CONVERSATION_SEND_IMAGE = 2;
    private static final int CONVERSATION_RECEIVE_IMAGE = 3;
    private static final int CONVERSATION_SEND_VOICE = 4;
    private static final int CONVERSATION_RECEIVE_VOICE = 5;
    private static final int CONVERSATION_SEND_VIDEO = 6;
    private static final int CONVERSATION_RECEIVE_VIDEO = 7;
    private static final int CONVERSATION_SEND_FILE = 8;
    private static final int CONVERSATION_RECEIVE_FILE = 9;
    private static final int CONVERSATION_SEND_LOCATION = 10;
    private static final int CONVERSATION_RECEIVE_LOCATION = 11;
    private static final int CONVERSATION_SEND_BUSINESS_CARD = 12;
    private static final int CONVERSATION_RECEIVE_BUSINESS_CARD = 13;
    private static final int CONVERSATION_SEND_SEQUENCE = 14;
    private static final int CONVERSATION_RECEIVE_SEQUENCE = 15;
    private static final int CONVERSATION_SEND_RECALL = 21;
    private static final int CONVERSATION_RECEIVE_RECALL = 22;
    private static final int CONVERSATION_TEXT = 60;

    public ConversationAdapter(Context context, List<MessageInfoUI> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getItemViewType(int position) {
        MessageInfo messageEntity = list.get(position).getMessageInfo();
        if (messageEntity.isSent) {
            if (messageEntity.isRevoke)
                return CONVERSATION_SEND_RECALL;
            switch (messageEntity.messageType) {
                case TEXT:
                    return CONVERSATION_SEND_TEXT;
                case IMAGE:
                    return CONVERSATION_SEND_IMAGE;
                case VOICE:
                    return CONVERSATION_SEND_VOICE;
                case VIDEO:
                    return CONVERSATION_SEND_VIDEO;
                case FILE:
                    return CONVERSATION_SEND_FILE;
                case CARD:
                    return CONVERSATION_SEND_BUSINESS_CARD;
                case LOCATION:
                    return CONVERSATION_SEND_LOCATION;
                case SEQUENCE:
                    return CONVERSATION_SEND_SEQUENCE;
//                case ENTERGROUP:
//                case ADDMEMBER:
//                case DELMEMBER:
//                case DROPGROUP:
//                case BEMASTER:
//                    return CONVERSATION_TEXT;
                default:
                    return CONVERSATION_TEXT;
            }
        } else {
            if (messageEntity.isRevoke)
                return CONVERSATION_RECEIVE_RECALL;
            switch (messageEntity.messageType) {
                case TEXT:
                    return CONVERSATION_RECEIVE_TEXT;
                case IMAGE:
                    return CONVERSATION_RECEIVE_IMAGE;
                case VOICE:
                    return CONVERSATION_RECEIVE_VOICE;
                case VIDEO:
                    return CONVERSATION_RECEIVE_VIDEO;
                case FILE:
                    return CONVERSATION_RECEIVE_FILE;
                case CARD:
                    return CONVERSATION_RECEIVE_BUSINESS_CARD;
                case LOCATION:
                    return CONVERSATION_RECEIVE_LOCATION;
                case SEQUENCE:
                    return CONVERSATION_RECEIVE_SEQUENCE;
//                case ENTERGROUP:
//                case ADDMEMBER:
//                case DELMEMBER:
//                case DROPGROUP:
//                case BEMASTER:
//                    return CONVERSATION_TEXT;
                default:
                    return CONVERSATION_TEXT;
            }
        }
    }

    public void setConversations(List<MessageInfoUI> messageEntities) {
        this.list.clear();
        this.list.addAll(messageEntities);
        notifyDataSetChanged();
    }

    public void loadMoreData(List<MessageInfoUI> messageEntities) {
        list.addAll(0, messageEntities);
        notifyItemRangeInserted(0, messageEntities.size());
    }

    public MessageInfoUI getItem(int position) {
        return list.get(position);
    }

    public void addNewConversation(MessageInfoUI message) {
        this.list.add(message);
        notifyItemInserted(this.list.size());
    }

    public void updateConversation(MessageInfoUI message) {
        int index = -1;
        for (int i = 0; i < this.list.size(); i++) {
            if (list.get(i).getMessageInfo().messageId.equals(message.getMessageInfo().messageId)) {
                index = i;
                break;
            }
        }
        if (index > -1) {
            notifyItemChanged(index);
        }
    }

    public void recall(String msgId) {
        int index = -1;
        for (int i = 0; i < this.list.size(); i++) {
            MessageInfoUI uiMessage = list.get(i);
            if (uiMessage.getRemoteMsgId().equals(msgId)) {
                index = i;
                uiMessage.getMessageInfo().isRevoke = true;
                break;
            }
        }
        if (index > -1) {
            notifyItemChanged(index);
        }
    }

    public void delete(String msgId) {
        int index = -1;
        for (int i = 0; i < this.list.size(); i++) {
            MessageInfoUI uiMessage = list.get(i);
            if (uiMessage.getRemoteMsgId().equals(msgId)) {
                index = i;
                break;
            }
        }
        if (index > -1) {
            this.list.remove(index);
            notifyItemRemoved(index);
        }
    }

    public List<MessageInfoUI> getMessages() {
        return list;
    }

    @NonNull
    @Override
    public MessageContentHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == CONVERSATION_SEND_TEXT) {
            View view = LayoutInflater.from(context).inflate(R.layout.conversation_item_send_text, parent, false);
            return new SendTextViewHolder(context, view, this);
        } else if (viewType == CONVERSATION_RECEIVE_TEXT) {
            View view = LayoutInflater.from(context).inflate(R.layout.conversation_item_receive_text, parent, false);
            return new ReceiveTextViewHolder(context, view, this);
        } else if (viewType == CONVERSATION_SEND_IMAGE) {
            View view = LayoutInflater.from(context).inflate(R.layout.conversation_item_send_image, parent, false);
            return new SendImageViewHolder(context, view, this);
        } else if (viewType == CONVERSATION_RECEIVE_IMAGE) {
            View view = LayoutInflater.from(context).inflate(R.layout.conversation_item_receive_image, parent, false);
            return new ReceiveImageViewHolder(context, view, this);
        } else if (viewType == CONVERSATION_SEND_VOICE) {
            View view = LayoutInflater.from(context).inflate(R.layout.conversation_item_send_voice, parent, false);
            return new SendAudioHolder(context, view, this);
        } else if (viewType == CONVERSATION_RECEIVE_VOICE) {
            View view = LayoutInflater.from(context).inflate(R.layout.conversation_item_receive_voice, parent, false);
            return new ReceiveAudioHolder(context, view, this);
        } else if (viewType == CONVERSATION_SEND_VIDEO) {
            View view = LayoutInflater.from(context).inflate(R.layout.conversation_item_send_video, parent, false);
            return new SendVideoHolder(context, view, this);
        } else if (viewType == CONVERSATION_RECEIVE_VIDEO) {
            View view = LayoutInflater.from(context).inflate(R.layout.conversation_item_receive_video, parent, false);
            return new ReceiveVideoHolder(context, view, this);
        } else if (viewType == CONVERSATION_SEND_FILE) {
            View view = LayoutInflater.from(context).inflate(R.layout.conversation_item_send_file, parent, false);
            return new SendFileHolder(context, view, this);
        } else if (viewType == CONVERSATION_RECEIVE_FILE) {
            View view = LayoutInflater.from(context).inflate(R.layout.conversation_item_receive_file, parent, false);
            return new ReceiveFileHolder(context, view, this);
        } else if (viewType == CONVERSATION_SEND_BUSINESS_CARD) {
            View view = LayoutInflater.from(context).inflate(R.layout.conversation_item_send_card, parent, false);
            return new SendCardHolder(context, view, this);
        } else if (viewType == CONVERSATION_RECEIVE_BUSINESS_CARD) {
            View view = LayoutInflater.from(context).inflate(R.layout.conversation_item_receive_card, parent, false);
            return new ReceiveCardHolder(context, view, this);
        } else if (viewType == CONVERSATION_SEND_SEQUENCE) {
            View view = LayoutInflater.from(context).inflate(R.layout.conversation_item_send_sequence, parent, false);
            return new SendSequenceViewHolder(context, view, this);
        } else if (viewType == CONVERSATION_RECEIVE_SEQUENCE) {
            View view = LayoutInflater.from(context).inflate(R.layout.conversation_item_receive_sequence, parent, false);
            return new ReceiveSequenceViewHolder(context, view, this);
        } else if (viewType == CONVERSATION_RECEIVE_RECALL || viewType == CONVERSATION_SEND_RECALL) {
            View view = LayoutInflater.from(context).inflate(R.layout.conversation_item_text, parent, false);
            return new RecallHolder(context, view, this);
        } else if (viewType == CONVERSATION_TEXT) {
            View view = LayoutInflater.from(context).inflate(R.layout.conversation_item_text, parent, false);
            return new TextViewHolder(context, view, this);
        } else {
            return null;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull MessageContentHolder holder, int position) {
        MessageInfoUI item = list.get(position);
        holder.bindView(item, position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public ConversationViewModel getConversationViewModel() {
        return conversationViewModel;
    }

    public void setConversationModel(ConversationViewModel conversationViewModel) {
        this.conversationViewModel = conversationViewModel;
    }
}
